package ibase.solr.tld;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.util.NamedList;

public class SolrTld implements Tag{
	
	Tag parent;
	PageContext pageContext ;

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		QueryResponse queryResponse = (QueryResponse) pageContext.getSession().getAttribute("solrResponse");
		SolrDocumentList docs = queryResponse.getResults();
		String uri=(String) pageContext.getSession().getAttribute("requestUri");
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		
		try{
		
		pageContext.getOut().write("Ashish Sonawane");
		pageContext.getOut().write("<div id =\"gridCenter\" >");
		pageContext.getOut().write("<div id =\"filterPane\" >");
		List<FacetField> facetFields = queryResponse.getFacetFields();

		Iterator<FacetField> facetFieldIterator = facetFields.iterator();
		
		while(facetFieldIterator.hasNext()) {
			FacetField facetField = facetFieldIterator.next();
			pageContext.getOut().write("<BR><li>" + facetField.getName());
			
			pageContext.getOut().write("<ul>");
			List<FacetField.Count> facetFieldCounts = facetField.getValues();
			Iterator<FacetField.Count> facetFieldCountIterator = facetFieldCounts.iterator();

			boolean hasQueryString = false;
			while(facetFieldCountIterator.hasNext()) {
				FacetField.Count count = facetFieldCountIterator.next();
				StringBuffer stringBuffer = new StringBuffer("?");

				if(request.getQueryString() != null) {
					stringBuffer.append(request.getQueryString());
					hasQueryString = true;
				}
				
				if(hasQueryString) {
					stringBuffer.append("&");
				}
				
				stringBuffer.append(facetField.getName() + "=" + count.getName());
				
				System.out.println("@@@@ string "+uri + stringBuffer.toString());
				System.out.println("@@@@ request.getRequestURI() in tld : ["+uri+" ]");

				String removeLink = null;
				if(null != request.getQueryString()) {
					removeLink = uri + "?" + request.getQueryString();
					removeLink = removeLink.replaceAll(facetField.getName() + "=" + count.getName(), "");
				}
				
				if(null != request.getQueryString() && request.getQueryString().contains(facetField.getName() + "=" + count.getName())) 
				{
					System.out.println("Inside if...");
					pageContext.getOut().write("<li>" + count.getName() + "(" + count.getCount() + ")<a href=\"" + removeLink + "\">(remove)</a></li>");
				} 
				else 
				{
					System.out.println("Inside else...");
					if(count.getCount() > 0 )
					{	
						String tmp = "http://192.168.0.102:9090" + uri + stringBuffer.toString();
						System.out.println(tmp);
						//http://192.168.0.102:9090/SolrQMSApllication/SearchServlet
						pageContext.getOut().write("<li><a onclick=\"raj('http://192.168.0.102:9090/SolrQMSApllication/SearchServlet?SearchValue=SSS&SITE_CODE__MFG=UT204');\" href=\"#\" >" + count.getName() + "(" + count.getCount() + ")</a></li>");
					}	
				}
				
			}
			pageContext.getOut().write("</ul></li>");
		
		}
		pageContext.getOut().write("</div>");
		pageContext.getOut().write("<iframe id='searchResult'>");
		if (docs != null)
		{
			pageContext.getOut().write(docs.getNumFound() + " documents found, "+ docs.size() + " returned : <BR>");
			for (int i = 0; i < docs.size(); i++) {
				SolrDocument doc = docs.get(i);
				pageContext.getOut().write("<BR><BR>" + doc.toString());
			}
		}
		pageContext.getOut().write("</iframe>");		
		pageContext.getOut().write("</div>");
			
		}
		catch(IOException IOE)
		{
			IOE.printStackTrace();
		}
		
		return EVAL_PAGE;
		
	}

	@Override
	public Tag getParent() {
		// TODO Auto-generated method stub
		return parent;
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setPageContext(PageContext pageContext) {
		// TODO Auto-generated method stub
		this.pageContext=pageContext;
	}

	@Override
	public void setParent(Tag parent) {
		// TODO Auto-generated method stub
		this.parent=parent;
	}
	
	
}
